<?php require_once(CONTROLLER_PATH . 'admin/Admin_master_controller.php');
class Users extends Admin_master_controller {
	
	private $_page_uri = "users";
	
	function __construct() {
		parent::__construct();
		//$this->load->model('DNN_model', '', TRUE);
		$this->load->model('dx_auth/users', 'users');
		//$this->dx_auth->ci->load->model('dx_auth/users', 'users_model');
		$this->_page_uri = admin_url() . $this->_page_uri;
		$this->view_data['page_uri'] = $this->_page_uri;
		$this->view_data['moduleTitle'] = "User Management";
	}
	function index() {
		$this->view_data['view'] = ADMIN_PATH . "users_list";
		$this->view_data['offset'] = 0;
		$this->view_data['page_title'] = 'List of Users';
		$this->view_data['page_title_class'] = 'header icon-48-config';
		//$this->load->model('dx_auth/users', 'users_model');
		$data_list = $this->users->get_all();
		$this->view_data['table'] = $this->_render_table($data_list, 0);
		$this->view_data['pagination'] = $this->_render_paging($this->dx_auth->ci->users->get_all(), 0);
		$this->render_page($this->view_data);
	}
	
	//Private Functions
	function _render_table($data_list, $offset = 0) {
		// generate table data
		$this->load->library('table');
		$this->table->set_empty("&nbsp;");
		$this->table->set_heading('No', 'Username', 'Password', 'Actions');
		$i = $offset;
		foreach ($data_list as $data_item) {
			$this->table->add_row(++$i, $data_item->username, $data_item->password, 
				anchor($this->_page_uri.'/view/'.$data_item->id . '/' . $offset ,'view',array('class'=>'view')).' '.
				anchor($this->_page_uri.'/edit/'.$data_item->id . '/' . $offset ,'edit',array('class'=>'update')).' '.
				anchor($this->_page_uri.'/delete/'.$data_item->id . '/' . $offset ,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this user?')"))
			);
		}
		return $this->table->generate();
	}
	
	function _render_paging($count_all, $offset = 0) {
		$this->load->library('pagination');
		//Paging Config
 		$page_config['per_page'] = ADMIN_ITEM_PER_PAGE;
		$page_config['base_url'] = $this->_page_uri.'/index/';
 		$page_config['total_rows'] = $count_all;
		$page_config['cur_page'] = $offset;
		$this->pagination->initialize($page_config);
		return $this->pagination->create_links();
	}
	
	function add($offset = 0) {
		// set common properties
		$this->view_data['view'] = ADMIN_PATH . "users_edit";
		$this->view_data['page_title'] = 'Add new User';
		$this->view_data['page_title_class'] = 'header icon-48-config';
		$this->view_data['action'] = $this->_page_uri . '/add/' . $offset;
		$this->view_data['link_back'] = $this->_page_uri . '/index/' . $offset;
		// set validation properties
		$this->_set_fields();
		$this->_set_rules();
		// run validation
		if ($this->validation->run() == FALSE) {
			$this->view_data['message'] = '';
		} else {
			$data_arr = array (
				'username' => $this->input->post('username'),
				'password' => $this->input->post('password'),
			);
			$id = $this->users_model->save($data_arr, $this->view_data['lang_arr']);
			//$this->validation->id = $id;
			$this->view_data['message'] = 'News item has been added successfully, Please edit data to add a new one';
		}
		$this->render_page($this->view_data);
	}
	
	// validation fields
	function _set_fields() {
		$fields['username'] = 'username';
		$fields['password'] = 'password';
		$this->validation->set_fields($fields);
	}
	//validation rules
	function _set_rules() {
		$rules['username'] = 'trim|required';
		$rules['password'] = 'trim|required';
		$this->validation->set_rules($rules);
		$this->validation->set_message('required', 'This field is required');
		$this->validation->set_error_delimiters('<p class="error">', '</p>');
	}
}
